package my.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberUtil {
   private static Map<String,Integer> numbers;
   static{
      numbers = new HashMap<String,Integer>();
      numbers.put("一", 1);
      numbers.put("二", 2);
      numbers.put("两", 2);
      numbers.put("三", 3);
      numbers.put("四", 4);
      numbers.put("五", 5);
      numbers.put("六", 6);
      numbers.put("七", 7);
      numbers.put("八", 8);
      numbers.put("九", 9);
      numbers.put("十", 10);
      numbers.put("百", 100);
      numbers.put("千", 1000);
   }
   public static int parseNumber(String num){
	   int result = -1;
	   if(StringUtil.isNULL(num))
		   return result;
	   Pattern compile1 = Pattern.compile("^[0-9]*$");
	   Pattern compile2 = Pattern.compile("^[零一二两三四五六七八九十百千]*$");
	   Matcher matcher1 = compile1.matcher(num);
	   Matcher matcher2 = compile2.matcher(num);
	   boolean find1 = matcher1.find();
	   boolean find2 = matcher2.find();
	   if(find1){
		   result = Integer.parseInt(num);
	   }else if(find2){
		   result = chinese2number(num);
	   }else{
		   result = -1;
	   }
	   return result;
   }
   public static int chinese2number(String num){
      StringBuilder str = new StringBuilder(num);
      Pattern compile1 = Pattern.compile("[一二两三四五六七八九][十百千]");
      Pattern compile2 = Pattern.compile("[一二两三四五六七八九十]");
      int num1 = patternNumber(compile1, str);
      int num2 = patternNumber(compile2, str);
      return num1+num2;
   }
   public static int patternNumber(Pattern compile,StringBuilder num){
      String string = num.toString();
      Matcher matcher = compile.matcher(string);
      int num_ = 0;
      while(matcher.find()){
        String group = matcher.group();
        num_ += calculate(group);
        int indexOf = num.indexOf(group);
        num.delete(indexOf,indexOf+ group.length());
      }
      return num_;
   }
   public static int calculate(String num){
      if(num.length()>1){
        int sum = 1;
        for(int i=0;i<num.length();i++){
           sum*=numbers.get(num.charAt(i)+"");
        }
        return sum;
      }else if(num.length()==1){
        return numbers.get(num);
      }
      return 0;
   }
   /**
    * 将数字限定在指定范围内
    * @param min 最小数字
    * @param max 最大数字
    * @param now 当前数字
    * @return 指定范围内的数字
    */
   public static int getNumber(int min,int max,int now){
	   if(now>max){
		   return max;
	   }
	   if(now<min){
		   return min;
	   }
	   return now;
   }
   
   public static void main(String[] args) {
      try {
		System.out.println(NumberUtil.parseNumber("一百一十"));
		System.out.println(NumberUtil.parseNumber("一百"));
		System.out.println(NumberUtil.parseNumber("两百一十"));
		System.out.println(NumberUtil.parseNumber("一百一十五"));
		System.out.println(NumberUtil.parseNumber("十五"));
		System.out.println(NumberUtil.parseNumber("16"));
		System.out.println(NumberUtil.parseNumber("1"));
		System.out.println(NumberUtil.parseNumber("1百"));
		System.out.println(NumberUtil.parseNumber("零"));
	} catch (Exception e) {
		e.printStackTrace();
	}
   }
   
}
